package com.skycong.hello77.activity.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.skycong.hello77.R;


/**
 * @author ruanmingcong
 * @date 2018/1/11 21:09
 */
public class NotificationActivity extends Activity implements View.OnClickListener{
    private Button sendInformation;
    private Button cancelInformation;
    NotificationManager manager;//通知控制类
    int nofication_Id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify);
        manager =   (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        sendInformation = (Button) findViewById(R.id.send_information);
        cancelInformation = (Button) findViewById(R.id.cancel_information);
        sendInformation.setOnClickListener(this);
        cancelInformation.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.send_information:{
                sendInformation();
                break;
            }
            case R.id.cancel_information:{
                manager.cancel(nofication_Id);//取消通知
                break;
            }
        }

    }
    /**
     * 构造notification并发送通知
     * */

    private void sendInformation() {
        Intent intent = new Intent(this,NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        Notification.Builder builder = new Notification.Builder(this);//使用构建着模式
        builder.setSmallIcon(R.mipmap.icon_touxiang);//设置图标
        builder.setTicker("通知");//手机状态栏的提示
        builder.setWhen(System.currentTimeMillis());//设置时间
        builder.setContentTitle("你有一封未读的短信");//设置标题
        builder.setContentText("我来自notification通知");//设置通知内容
        builder.setContentIntent(pendingIntent);//设置点击意图
        //以下都需要权限
        builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音
        builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
        builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
        //builder.setDefaults(Notification.DEFAULT_ALL);//设置声音，震动，指示灯
        Notification notification = builder.build();//获取Notification
        manager.notify(nofication_Id,notification);
    }
}
